﻿using System;
using UnityEngine;
using UnityEditor;
namespace UtilEditors
{
    /// <summary>
    /// 弹框提示
    /// </summary>
    public class Popout : EditorWindow
    {
        public static Popout window;
        private static string customTxt;
        private static Action[] action;
        static int Num = 0;
        public static void ShowWindow(string _title, string message, params Action[] act)
        {
            if (window == null)
            {
                window = (Popout)GetWindow(typeof(Popout));
            }
            action = act;
            if (action != null)
            {
                Num = action.Length;
            }
            window.titleContent.text = _title;
            customTxt = message;
            window.position = new Rect(1920 / 2 - 150, 1080 / 2 - 50, 300, 100);
            window.Show();
        }

        void OnGUI()
        {
            GUILayout.FlexibleSpace();
            {
                GUILayout.Label(customTxt);
            }
            GUILayout.FlexibleSpace();
            GUILayout.BeginHorizontal();
            {
                GUILayout.FlexibleSpace();
                {
                    if (GUILayout.Button("确定", GUILayout.Width(80), GUILayout.Height(40)))
                    {
                        window.Close();
                        if (Num > 0)
                        {
                            action[0].Invoke();
                        }
                    }

                    if (Num > 1)
                    {
                        if (GUILayout.Button("取消", GUILayout.Width(80), GUILayout.Height(40)))
                        {
                            window.Close();
                            action[1].Invoke();
                        }
                    }
                    if (Num > 2)
                    {
                        if (GUILayout.Button("停止", GUILayout.Width(80), GUILayout.Height(40)))
                        {
                            window.Close();
                            action[2].Invoke();
                        }
                    }
                }
                GUILayout.FlexibleSpace();
            }
            GUILayout.EndHorizontal();

        }

        public static void WindowHint(string message, string title = "提示", bool isPause = true, params Action[] action)
        {
            int index = action.Length;
            switch (index)
            {
                case 0:
                    if (isPause)
                    {
                        var isV = EditorUtility.DisplayDialog(title, message, "确认", "取消");
                        if (isV)
                        {
                            if (action.Length > 0)
                            {
                                if (action[0] != null)
                                {
                                    action[0].Invoke();
                                }
                            }
                        }
                        else
                        {
                            if (action.Length > 1)
                                if (action[1] != null)
                                    action[1].Invoke();
                        }
                    }
                    else
                    {
                        Popout.ShowWindow(title, message, action);
                    }


                    break;
                case 1:
                    if (isPause)
                    {
                        var isVs = EditorUtility.DisplayDialog(title, message, "确认", "取消");
                        if (isVs)
                        {
                            if (action.Length > 0)
                            {
                                if (action[0] != null)
                                {
                                    action[0].Invoke();
                                }
                            }
                        }
                        else
                        {
                            if (action.Length > 1)
                                if (action[1] != null)
                                    action[1].Invoke();
                        }
                    }
                    else
                    {
                        Popout.ShowWindow(title, message, action);
                    }
                    break;

                case 2:
                    if (isPause)
                    {
                        int option = EditorUtility.DisplayDialogComplex(title, message, "确认", "取消", "停止");
                        if (option == 0)
                        {
                            if (action[0] != null)
                                action[0].Invoke();
                        }
                        else if (option == 1)
                        {
                            if (action[1] != null)
                                action[1].Invoke();
                        }
                        else if (option == 2)
                        {
                            if (action[2] != null)
                                action[2].Invoke();
                        }
                    }
                    else
                    {
                        Popout.ShowWindow(title, message, action);
                    }
                    break;
            }
        }
    }
}